package com.hongning.railway.ui.main.fragments.cadremanagement;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.MettingMessageBean;
import com.hongning.railway.entity.reqbean.RosterBean;
import com.hongning.railway.entity.reqbean.RosterMainBean;
import com.hongning.railway.entity.reqbean.ThreesessionSanDoneLessonBean;
import com.hongning.railway.ui.main.fragments.home.itemviewmodel.ThreesessionSanDoneLessonMsgItemViewModel;
import com.hongning.railway.ui.main.fragments.threesessionsandonelesson.ThreesessionSanDoneLessonMsgViewModel;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CadreManagementRosterListViewModel  extends BaseViewModel<AppRepository> {
    public ObservableList<CadreManagementRosterListItemViewModel> viewlist=new ObservableArrayList<>();
    public ItemBinding<CadreManagementRosterListItemViewModel> itemBinding = ItemBinding.of(BR.rosterItemViewModel, R.layout.fragment_cadremanagementrosterlist_item);

    public CadreManagementRosterListViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }


    public void initDatas(MettingMessageBean mettingMessageBean){
        model.selectCadreList(mettingMessageBean)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("数据加载中...");
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<RosterMainBean>() {
            @Override
            public void onResult(RosterMainBean response) {
                  if (response==null||response.getData()==null||response.getData().isEmpty())
                  {
                      ToastUtils.showShort("未查询到数据");
                      return;
                  }
                  int i=0;
                for (RosterBean rosterBean:response.getData()) {
                    i++;
                    viewlist.add(new CadreManagementRosterListItemViewModel(CadreManagementRosterListViewModel.this,rosterBean,i));
                }
            }
        });
    }
}
